#!/usr/bin/env python

import re

class MLBConfig:

    def __init__(self, default_dct=dict()):
        self.MULTIPLE_VALUES = ('blackout', 'audio_follow', 'video_follow', 'favorite')
        self.BOOLEANS = ('show_player_command', 'debug', 'use_color', 'dvr_record_only', 'live_from_start', 'use_nexdef', 'strict_stream', 'show_inning_frames')
        self.data = default_dct
        self.file = None

    def loads(self, authfile):
        self.file = authfile
        fp = open(authfile)
        for line in fp:
            # Skip all the comments
            if line.startswith('#'):
                pass
            # Skip all the blank lines
            elif re.match(r'^\s*$',line):
                pass
            else:
            # Break at the first equals sign
                key, val = line.split('=')[0], '='.join(line.split('=')[1:])
                key = key.strip()
                val = val.strip()
                # These are the ones that take multiple values
                if key in self.MULTIPLE_VALUES:
                    self.data[key].append(val)
                # These are the booleans:
                elif key in self.BOOLEANS:
                    if val.isdigit():
                        self.data[key] = bool(int(val))
                    else:
                        if val.lower() in ('false', 'no', 'none'):
                            self.data[key] = False
                        elif val.lower() in ('true', 'yes'):
                            self.data[key] = True
                        # Otherwise stick with the default.
                        else:
                            pass
                # And these are the ones that only take one value, and so,
                # replace the defaults.
                else:
                    self.data[key] = val

    def write(self, file=None):
        if file == None:
            file = self.file
        fp = open(file,"w")
        for i in self.data.keys():
            if i in self.MULTIPLE_VALUES:
                for j in self.data[i]:
                    fp.write(i+"="+j+"\n")
            else:
                fp.write(i+"="+str(self.data[i])+"\n")
        fp.close()

